﻿/*
 * 合同的创建和修改。
 * 合同处于草稿状态时，才可以修改，其他状态，不能修改。 
2009-5-6 请详见最新的标志设计。
 * 
 * */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using EQT.Dal;
using EQT.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public partial class CreateContract : System.Web.UI.Page
{
    #region 数据访问成员
    private readonly DaClientInfo daci = new DaClientInfo(DaCommon.DefaultDb);
    private readonly DaContract dacontract = new DaContract(DaCommon.DefaultDb);
    private readonly DaTfile datf = new DaTfile(DaCommon.DefaultDb);
    private readonly DaRefFile darf = new DaRefFile(DaCommon.DefaultDb);
    private readonly DaSampleParameter dasp = new DaSampleParameter(DaCommon.DefaultDb); //样品参数
    private readonly DaGbinfo dagb = new DaGbinfo(DaCommon.DefaultDb);
    private readonly DaContractgb dacgb = new DaContractgb(DaCommon.DefaultDb);
    private readonly DaCoTestItem dacti = new DaCoTestItem(DaCommon.DefaultDb);//合同检验项
    private readonly DaSysCoding dasc = new DaSysCoding(DaCommon.DefaultDb);
    private readonly DaReportAudit dara = new DaReportAudit(DaCommon.DefaultDb);
    private readonly DaTestRecord datr = new DaTestRecord(DaCommon.DefaultDb);
    private readonly DaNotice danotice = new DaNotice(DaCommon.DefaultDb);
    private readonly DaDirFile dadf = new DaDirFile(DaCommon.DefaultDb);

    #endregion

    /* table1 客户信息
     * table2 检验依据 检验项目(业务组负责其内容，实验组根据这些内容上传原始记录)
     * table3 原始记录
     * table4 检验报告
     * table5 报告审核设置
     * table6 专家审核
     * table7 站长审核
     * **/

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            if (Request.UrlReferrer != null)   // 
            {
                ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
            }

            #region 合同创建和编辑
            if (String.IsNullOrEmpty(Request.QueryString["reportid"]))//新建合同
            {
                if (table1.Visible == false) table1.Visible = true; //仅客户信息可见
                #region 其他面板暂时不可见
                if (table2.Visible == true) table2.Visible = false;
                if (table3.Visible == true) table3.Visible = false;
                if (table4.Visible == true) table4.Visible = false;
                if (table5.Visible == true) table5.Visible = false;
                if (table6.Visible == true) table6.Visible = false;
                if (table7.Visible == true) table7.Visible = false;
                #endregion
            }
            else  //编辑已有的合同
            {
                //绑定此report_id
                //1、加载合同客户信息
                string report_id = Request.QueryString["reportid"];
                ViewState["REPORT_ID"] = report_id;//报告ID
                MoContract mo = dacontract.GetEntity(report_id);
                if (mo != null)
                {
                    SuitUI2ReportStatus(mo);
                }
            }
            #endregion
        }

    }



    #region 绑定客户信息
    private void BindClientInfo(MoContract mo)
    {
        if (table1.Visible)
        {
            //ViewState["CLIENT_ID"] = mo.Client_Id;
            textBoxClientID.Text = mo.Client_Id;
            textBoxCorp_Name.Text = mo.Corp_Name;
            textBoxConsigner.Text = mo.Consigner;
            textBoxLinkman.Text = mo.Linkman;
            textBoxPhone.Text = mo.Phone;
            textBoxFax.Text = mo.Fax;
            textBoxEmail.Text = mo.Email;


        }
    }
    #endregion

    #region 绑定检验依据
    /// <summary>
    /// 绑定依据
    /// </summary>
    private void BindContractGB()
    {
        if (table2.Visible)
        {
            GridViewGbInfo.DataSource = dagb.GetReportGB(ViewState["REPORT_ID"].ToString());
            GridViewGbInfo.DataBind();
        }
    }

    #endregion

    #region 绑定合同检验项
    /// <summary>
    /// 绑定合同检验项
    /// </summary>
    private void BindCoTestItem()
    {
        if (table2.Visible)
        {
            string report_id = ViewState["REPORT_ID"].ToString();
            MoContract mo = dacontract.GetEntity(report_id);
            //合同检验项
            GridViewCoTestItem.DataSource = dacti.GetCoTestItemDs(report_id);
            GridViewCoTestItem.DataBind();

            //价格Summery
            if (mo.Total_Price > 0)
            {
                LabelPriceSummary.Text = String.Format("合同签订额：{0,10:C}RMB/检验项价格合计：{1,10:C}RMB/折扣率：{2,10:F2}%", mo.Charge, mo.Total_Price, mo.Charge * 100 / mo.Total_Price);
            }



        }
    }
    #endregion

    #region 绑定参数文件
    /// <summary>
    /// 绑定参数文件
    /// </summary>
    private void BindParameterFiles()
    {
        string report_id = ViewState["REPORT_ID"].ToString();
        GridViewParametersFile.DataSource = dasp.GetSampleParameterFileInfo(report_id);
        GridViewParametersFile.DataBind();
    }
    #endregion

    #region 绑定原始记录文件
    /// <summary>
    /// 绑定原始记录
    /// </summary>
    private void BindTestRecords()
    {
        string report_id = ViewState["REPORT_ID"].ToString();

        GridViewTestRecords.DataSource = dacti.GetTestRecordsByReportId(report_id);
        GridViewTestRecords.DataBind();
        UserContext uc = Session["USER_CONTEXT"] as UserContext;
        //本组试验是否全部完成

        int todo = dacti.GetCount(String.Format(" WHERE Report_ID='{0}' AND Charge_Dept_Code='{1}' ", report_id, uc.DeptID));
        if (todo > 0)
        {
            int testdone = dacti.GetCount(String.Format(" WHERE Report_ID='{0}' AND Charge_Dept_Code='{1}' AND Test_Status=17 AND Test_Status<256", report_id, uc.DeptID));
            if (testdone == todo)
            {

                LabelTaskStatus.Text = String.Format("本部门【{0}】已完成合同{1}的{2}个中的{3}个任务。", uc.DeptName, report_id, todo, testdone);
                BtnConfirmTaskDone.Enabled = true;
            }
            else
            {
                //检查是否确认完成了
                int taskdone = dacti.GetCount(String.Format(" WHERE Report_ID='{0}' AND Charge_Dept_Code='{1}' AND Test_Status>17 AND Test_Status<256", report_id, uc.DeptID));
                if (taskdone == todo)
                {
                    LabelTaskStatus.Text = String.Format("本部门【{0}】负责的试验已经全部完成。", uc.DeptName);
                    BtnConfirmTaskDone.Visible = false;
                }
            }

        }
        else
        {
            LabelTaskStatus.Text = String.Format("合同{0}无本部门【{1}】试验任务。", report_id, uc.DeptName);
            if (uc.RoleID != "Administrators")
            {
                BtnConfirmTaskDone.Visible = false;
            }
        }
    }
    #endregion

    #region 绑定报告组上传的报告
    /// <summary>
    /// 当选择的合同发生改变时，会调用这个函数来绑定页面值.
    /// </summary>
    /// <param name="m"></param>
    private void BindReportInfo(MoContract m)
    {
        LabelReportId.Text = m.Report_Id;
        if (!String.IsNullOrEmpty(m.Report_Url))
        {
            MoTfile f = datf.GetEntity(m.Report_Url);
            LabelReportName.Text = Server.HtmlEncode(f.File_Name);
            HyperLinkReport.NavigateUrl = String.Format("~/getfile.aspx?fileid={0}", f.File_Id);
            HyperLinkReport.Text = Server.HtmlEncode(f.File_Id);
            //LabelUploader.Text=f. //TODO:应该改进文件管理模式，请考虑：将目录放到一个表中，将文件放到另一个表中。
            LabelUploadTime.Text = f.File_Create_Time.ToLongTimeString();
            if ((m.Report_Status & (int)ContractStatus.Clear_Pay_Flag) == (int)ContractStatus.Reporting)
            {
                BtnDeleteReport.Visible = true;
            }
        }
        else
        {
            LabelReportName.Text = String.Empty;
            LabelUploadTime.Text = String.Empty;
            HyperLinkReport.Text = Server.HtmlEncode("<<未上传最终报告>>");
            HyperLinkReport.NavigateUrl = "#";
            BtnDeleteReport.Visible = false;
        }
        //这里设置选择审批人的脚本
        BtnSelectExperts.Attributes.Add("OnClick", "InvokePopSelectAuditor('" + m.Report_Id + "','" + SiteConfig.REPORT_AUDIT_FLOW_1_EXPERT_AUDIT + "')");
        ButtonSelectMaster.Attributes.Add("OnClick", "InvokePopSelectAuditor('" + m.Report_Id + "','" + SiteConfig.REPORT_AUDIT_FLOW_2_MASTER_AUDIT + "')");
        //审批人列表，以字符串形式表达
        LabelExperts.Text = Server.HtmlEncode(dara.GetReportAuditorNames(m.Report_Id, SiteConfig.REPORT_AUDIT_FLOW_1_EXPERT_AUDIT));

        BtnUploadReport.Attributes.Add("OnClick", "InvokePopUploadFinalReport('" + m.Report_Id + "')");

    }
    #endregion

    #region 加载专家列表，以字符串形式，加载站长列表，以字符串形式
    /// <summary>
    /// 加载审核设置
    /// </summary>
    private void LoadAuditSetting()
    {
        //加载报告的审核专家,加载报告的审核站长
        LabelExperts.Text = Server.HtmlEncode(dara.GetReportAuditorNames(ViewState["REPORT_ID"].ToString(), SiteConfig.REPORT_AUDIT_FLOW_1_EXPERT_AUDIT));

        LabelMasters.Text = Server.HtmlEncode(dara.GetReportAuditorNames(ViewState["REPORT_ID"].ToString(), SiteConfig.REPORT_AUDIT_FLOW_2_MASTER_AUDIT));
    }
    #endregion

    #region 加载专家审核意见
    /// <summary>
    /// 加载专家审核意见
    /// </summary>
    private void LoadConirm1Rem()
    {
        MoReportAudit ra = dara.GetAuditInfo(ViewState["REPORT_ID"].ToString(), SiteConfig.REPORT_AUDIT_FLOW_1_EXPERT_AUDIT);
        if (ra != null)
        {
            TextBoxRem1.Text = ra.AuditInfo;
        }
    }
    #endregion

    #region 加载站长审核意见
    /// <summary>
    /// 加载站长审核意见
    /// </summary>
    private void LoadConfirm2Rem()
    {
        MoReportAudit ra = dara.GetAuditInfo(ViewState["REPORT_ID"].ToString(), SiteConfig.REPORT_AUDIT_FLOW_2_MASTER_AUDIT);
        if (ra != null)
        {
            TextBoxRem2.Text = ra.AuditInfo;
        }
    }
    #endregion

    #region 匹配报告状态到UI视图
    /// <summary>
    /// 匹配报告状态到UI视图
    /// </summary>
    /// <param name="reportStatus"></param>
    private void SuitUI2ReportStatus(MoContract mo)
    {
        int reportStatus = mo.Report_Status;
        string report_id = mo.Report_Id;
        LabelContractStatus.Text = dacontract.GetContractReportStateText(reportStatus);
        int rs = reportStatus & ((int)ContractStatus.Clear_Pay_Flag);
        ContractStatus cs = (ContractStatus)rs;
        BtnUploadReport.Attributes.Add("OnClick", "InvokePopUploadFinalReport('" + report_id + "')");



        #region 根据状态判断
        switch (cs)
        {
            case ContractStatus.Draft: //草稿，是绝对状态
                {
                    #region 仅业务组可以操作 ，其他元素不可见

                    table1.Visible = true;
                    table2.Visible = true;
                    #endregion
                    BtnRemoveContract.Visible = true;

                    table3.Visible = false;//原始记录不可见
                    table4.Visible = false; //检验报告
                    table5.Visible = false;
                    table6.Visible = false;
                    table7.Visible = false;

                    break;
                }
            case ContractStatus.Signed: //已签订
                {
                    #region 业务组元素
                    table1.Visible = true;
                    table2.Visible = true;
                    #endregion

                    #region 客户信息不可更改
                    textBoxCorp_Name.ReadOnly = true;
                    textBoxConsigner.ReadOnly = true;
                    textBoxLinkman.ReadOnly = true;
                    textBoxPhone.ReadOnly = true;
                    textBoxFax.ReadOnly = true;
                    textBoxEmail.ReadOnly = true;
                    BtnReset.Visible = false;//客户信息重置不允许
                    BtnSelectClient.Visible = false;//选择客户部允许
                    BtnGoToSetp2.Visible = false;//保存客户信息不允许
                    btnSelectTestItem.Visible = false;//选择测试项不允许
                    BtnFixContract.Visible = false;//签订合同按钮不允许
                    #endregion

                    #region 不可删除样品参数 不可移除检验依据 不可删减检验项目 不可上传原始记录
                    table2.Visible = true;
                    GridViewParametersFile.Columns[4].Visible = false;//不可删除参数文件
                    btnUploadParFile.Visible = false;                 //不准再上传参数文件
                    BtnUploadParFileHidden.Visible = false;


                    GridViewGbInfo.Columns[2].Visible = false;//不可移除检验依据
                    BtnSelectGbInfo.Visible = false;
                    //GridViewCoTestItem.Columns[5].Visible = false;//未送检，不可上传原始记录
                    //GridViewCoTestItem.Columns[4].Visible = false;//不可删减检验项目


                    #endregion

                    #region 其他不可见
                    table3.Visible = false;//原始记录不可见
                    table4.Visible = false; //检验报告
                    table5.Visible = false;
                    table6.Visible = false;
                    table7.Visible = false;
                    #endregion

                    BtnRemoveContract.Visible = true;
                    break;
                }
            case ContractStatus.WaitTest://已送检
                {
                    #region  客户信息不可更改
                    table1.Visible = true;
                    table2.Visible = true;
                    textBoxCorp_Name.ReadOnly = true;
                    textBoxConsigner.ReadOnly = true;
                    textBoxLinkman.ReadOnly = true;
                    textBoxPhone.ReadOnly = true;
                    textBoxFax.ReadOnly = true;
                    textBoxEmail.ReadOnly = true;
                    BtnReset.Visible = false;//客户信息重置不允许
                    BtnSelectClient.Visible = false;//选择客户部允许
                    BtnGoToSetp2.Visible = false;//保存客户信息不允许
                    btnSelectTestItem.Visible = false;//选择测试项不允许
                    BtnFixContract.Visible = false;//签订合同按钮不允许
                    #endregion

                    #region 不可删除样品参数 不可移除检验依据 不可删减检验项目 可上传原始记录
                    table2.Visible = true;
                    GridViewParametersFile.Columns[4].Visible = false;//不可删除参数文件
                    btnUploadParFile.Visible = false;                 //不准再上传参数文件
                    BtnUploadParFileHidden.Visible = false;


                    GridViewGbInfo.Columns[2].Visible = false;//不可移除检验依据
                    BtnSelectGbInfo.Visible = false;
                    GridViewCoTestItem.Columns[4].Visible = false;//不可删减检验项目

                    #endregion

                    #region 其他不可见
                    table3.Visible = false;//原始记录
                    table4.Visible = false; //检验报告
                    table5.Visible = false;
                    table6.Visible = false;
                    table7.Visible = false;
                    #endregion
                    break;
                }
            case ContractStatus.Testing://检验中
                {
                    #region  客户信息不可更改
                    table1.Visible = true;
                    table2.Visible = true;
                    textBoxCorp_Name.ReadOnly = true;
                    textBoxConsigner.ReadOnly = true;
                    textBoxLinkman.ReadOnly = true;
                    textBoxPhone.ReadOnly = true;
                    textBoxFax.ReadOnly = true;
                    textBoxEmail.ReadOnly = true;
                    BtnReset.Visible = false;//客户信息重置不允许
                    BtnSelectClient.Visible = false;//选择客户部允许
                    BtnGoToSetp2.Visible = false;//保存客户信息不允许
                    btnSelectTestItem.Visible = false;//选择测试项不允许
                    BtnFixContract.Visible = false;//签订合同按钮不允许
                    #endregion

                    #region 不可删除样品参数 不可移除检验依据 不可删减检验项目 可上传原始记录
                    table2.Visible = true;
                    GridViewParametersFile.Columns[4].Visible = false;//不可删除参数文件
                    btnUploadParFile.Visible = false;                 //不准再上传参数文件
                    BtnUploadParFileHidden.Visible = false;


                    GridViewGbInfo.Columns[2].Visible = false;//不可移除检验依据
                    BtnSelectGbInfo.Visible = false;
                    GridViewCoTestItem.Columns[4].Visible = true;//不可删减检验项目

                    #endregion
                    table3.Visible = true;//原始记录
                    GridViewTestRecords.Visible = true;
                    GridViewTestRecords.Columns[6].Visible = true;

                    #region 其他不可见
                    table4.Visible = false; //检验报告
                    table5.Visible = false;
                    table6.Visible = false;
                    table7.Visible = false;
                    #endregion
                    break;
                }
            case ContractStatus.WaitReport: //检验完成
                {
                    #region  客户信息不可更改
                    table1.Visible = true;
                    table2.Visible = true;
                    textBoxCorp_Name.ReadOnly = true;
                    textBoxConsigner.ReadOnly = true;
                    textBoxLinkman.ReadOnly = true;
                    textBoxPhone.ReadOnly = true;
                    textBoxFax.ReadOnly = true;
                    textBoxEmail.ReadOnly = true;
                    BtnReset.Visible = false;//客户信息重置不允许
                    BtnSelectClient.Visible = false;//选择客户部允许
                    BtnGoToSetp2.Visible = false;//保存客户信息不允许
                    btnSelectTestItem.Visible = false;//选择测试项不允许
                    BtnFixContract.Visible = false;//签订合同按钮不允许
                    #endregion

                    #region 不可删除样品参数 不可移除检验依据 不可删减检验项目 不可上传原始记录
                    table2.Visible = true;
                    GridViewParametersFile.Columns[4].Visible = false;//不可删除参数文件
                    btnUploadParFile.Visible = false;                 //不准再上传参数文件
                    BtnUploadParFileHidden.Visible = false;


                    GridViewGbInfo.Columns[2].Visible = false;//不可移除检验依据
                    BtnSelectGbInfo.Visible = false;
                    GridViewCoTestItem.Columns[4].Visible = false;//已完成 合同检验项不可再操作
                    #endregion

                    table3.Visible = true;//原始记录
                    GridViewTestRecords.Columns[6].Visible = false; //原始记录不可再操作
                    table4.Visible = true; //检验报告
                    table5.Visible = true;//审核设置
                    #region 其他不可见
                    table6.Visible = false;
                    table7.Visible = false;
                    #endregion
                    break;
                }
            case ContractStatus.Reporting://报告整理中
                {
                    #region  客户信息不可更改
                    table1.Visible = true;
                    table2.Visible = true;
                    textBoxCorp_Name.ReadOnly = true;
                    textBoxConsigner.ReadOnly = true;
                    textBoxLinkman.ReadOnly = true;
                    textBoxPhone.ReadOnly = true;
                    textBoxFax.ReadOnly = true;
                    textBoxEmail.ReadOnly = true;
                    BtnReset.Visible = false;//客户信息重置不允许
                    BtnSelectClient.Visible = false;//选择客户部允许
                    BtnGoToSetp2.Visible = false;//保存客户信息不允许
                    btnSelectTestItem.Visible = false;//选择测试项不允许
                    BtnFixContract.Visible = false;//签订合同按钮不允许
                    #endregion

                    #region 不可删除样品参数 不可移除检验依据 不可删减检验项目 不可上传原始记录
                    table2.Visible = true;
                    GridViewParametersFile.Columns[4].Visible = false;//不可删除参数文件
                    btnUploadParFile.Visible = false;                 //不准再上传参数文件
                    BtnUploadParFileHidden.Visible = false;


                    GridViewGbInfo.Columns[2].Visible = false;//不可移除检验依据
                    BtnSelectGbInfo.Visible = false;
                    GridViewCoTestItem.Columns[4].Visible = false;//不可删减检验项目

                    #endregion

                    table3.Visible = true;//原始记录
                    GridViewTestRecords.Columns[6].Visible = false; //原始记录不可删除

                    table4.Visible = true; //检验报告
                    table5.Visible = true;//审核设置
                    #region 其他不可见

                    table6.Visible = false;
                    table7.Visible = false;
                    #endregion
                    break;
                }
            case ContractStatus.WaitConfirm1://报告整理完成
                {
                    #region  客户信息不可更改
                    table1.Visible = true;
                    table2.Visible = true;
                    textBoxCorp_Name.ReadOnly = true;
                    textBoxConsigner.ReadOnly = true;
                    textBoxLinkman.ReadOnly = true;
                    textBoxPhone.ReadOnly = true;
                    textBoxFax.ReadOnly = true;
                    textBoxEmail.ReadOnly = true;
                    BtnReset.Visible = false;//客户信息重置不允许
                    BtnSelectClient.Visible = false;//选择客户部允许
                    BtnGoToSetp2.Visible = false;//保存客户信息不允许
                    btnSelectTestItem.Visible = false;//选择测试项不允许
                    BtnFixContract.Visible = false;//签订合同按钮不允许
                    #endregion

                    #region 不可删除样品参数 不可移除检验依据 不可删减检验项目 不可上传原始记录
                    table2.Visible = true;
                    GridViewParametersFile.Columns[4].Visible = false;//不可删除参数文件
                    btnUploadParFile.Visible = false;                 //不准再上传参数文件
                    BtnUploadParFileHidden.Visible = false;


                    GridViewGbInfo.Columns[2].Visible = false;//不可移除检验依据
                    BtnSelectGbInfo.Visible = false;
                    GridViewCoTestItem.Columns[4].Visible = false;// 不可上传原始记录

                    #endregion

                    #region 3，4，5不可改变
                    table3.Visible = true;//原始记录
                    GridViewTestRecords.Columns[6].Visible = false; //原始记录不可删除
                    table4.Visible = true; //检验报告                   
                    table5.Visible = true;//审核设置 

                    #endregion
                    table6.Visible = true;//专家审核

                    #region 已整理完成的报告不能再修改
                    BtnToWaitConfirm1.Enabled = false;
                    BtnUploadReport.Enabled = false;
                    BtnSendBackTestGrp.Enabled = false;
                    BtnDeleteReport.Enabled = false;
                    BtnSelectExperts.Visible = false;
                    ButtonSelectMaster.Visible = false;
                    #endregion

                    table7.Visible = false;
                    break;
                }
            case ContractStatus.Confirming1://专家审核中
                {

                    #region  客户信息不可更改
                    table1.Visible = true;
                    table2.Visible = true;
                    textBoxCorp_Name.ReadOnly = true;
                    textBoxConsigner.ReadOnly = true;
                    textBoxLinkman.ReadOnly = true;
                    textBoxPhone.ReadOnly = true;
                    textBoxFax.ReadOnly = true;
                    textBoxEmail.ReadOnly = true;
                    BtnReset.Visible = false;//客户信息重置不允许
                    BtnSelectClient.Visible = false;//选择客户部允许
                    BtnGoToSetp2.Visible = false;//保存客户信息不允许
                    btnSelectTestItem.Visible = false;//选择测试项不允许
                    BtnFixContract.Visible = false;//签订合同按钮不允许
                    #endregion

                    #region 不可删除样品参数 不可移除检验依据 不可删减检验项目 不可上传原始记录
                    table2.Visible = true;
                    GridViewParametersFile.Columns[4].Visible = false;//不可删除参数文件
                    btnUploadParFile.Visible = false;                 //不准再上传参数文件
                    BtnUploadParFileHidden.Visible = false;


                    GridViewGbInfo.Columns[2].Visible = false;//不可移除检验依据
                    BtnSelectGbInfo.Visible = false;

                    GridViewCoTestItem.Columns[4].Visible = false;//不可删减检验项目

                    #endregion

                    #region 3，4，5不可改变
                    table3.Visible = true;//原始记录
                    GridViewTestRecords.Columns[6].Visible = false; //原始记录不可删除
                    table4.Visible = true; //检验报告

                    table5.Visible = true;//审核设置 

                    #endregion
                    table6.Visible = true;//专家审核                     
                    table7.Visible = false;

                    #region 已整理完成的报告不能再修改
                    BtnToWaitConfirm1.Enabled = false;
                    BtnUploadReport.Enabled = false;
                    BtnSendBackTestGrp.Enabled = false;
                    BtnDeleteReport.Enabled = false;
                    BtnSelectExperts.Visible = false;
                    ButtonSelectMaster.Visible = false;
                    #endregion

                    break;
                }
            case ContractStatus.WaitLastConfirm://专家审核完成
                {

                    #region  客户信息不可更改
                    table1.Visible = true;
                    table2.Visible = true;
                    textBoxCorp_Name.ReadOnly = true;
                    textBoxConsigner.ReadOnly = true;
                    textBoxLinkman.ReadOnly = true;
                    textBoxPhone.ReadOnly = true;
                    textBoxFax.ReadOnly = true;
                    textBoxEmail.ReadOnly = true;
                    BtnReset.Visible = false;//客户信息重置不允许
                    BtnSelectClient.Visible = false;//选择客户部允许
                    BtnGoToSetp2.Visible = false;//保存客户信息不允许
                    btnSelectTestItem.Visible = false;//选择测试项不允许
                    BtnFixContract.Visible = false;//签订合同按钮不允许
                    #endregion

                    #region 不可删除样品参数 不可移除检验依据 不可删减检验项目 不可上传原始记录
                    table2.Visible = true;
                    GridViewParametersFile.Columns[4].Visible = false;//不可删除参数文件
                    btnUploadParFile.Visible = false;                 //不准再上传参数文件
                    BtnUploadParFileHidden.Visible = false;


                    GridViewGbInfo.Columns[2].Visible = false;//不可移除检验依据
                    BtnSelectGbInfo.Visible = false;

                    GridViewCoTestItem.Columns[4].Visible = false;//不可删减检验项目

                    #endregion

                    #region 3，4，5不可改变
                    table3.Visible = true;//原始记录
                    GridViewTestRecords.Columns[6].Visible = false; //原始记录不可删除
                    table4.Visible = true; //检验报告
                    table5.Visible = true;//审核设置               

                    #endregion

                    #region 6 专家审核信息不可改
                    table6.Visible = true;//专家审核
                    BtnConfirm1.Enabled = false;
                    TextBoxRem1.ReadOnly = true;
                    BtnRefuse1.Enabled = false;
                    #endregion

                    #region 已整理完成的报告不能再修改
                    BtnToWaitConfirm1.Enabled = false;
                    BtnUploadReport.Enabled = false;
                    BtnSendBackTestGrp.Enabled = false;
                    BtnDeleteReport.Enabled = false;
                    BtnSelectExperts.Visible = false;
                    ButtonSelectMaster.Visible = false;
                    #endregion
                    table7.Visible = true;
                    break;
                }
            case ContractStatus.LastConfirming://最终审核中
                {

                    #region  客户信息不可更改
                    table1.Visible = true;
                    table2.Visible = true;
                    textBoxCorp_Name.ReadOnly = true;
                    textBoxConsigner.ReadOnly = true;
                    textBoxLinkman.ReadOnly = true;
                    textBoxPhone.ReadOnly = true;
                    textBoxFax.ReadOnly = true;
                    textBoxEmail.ReadOnly = true;
                    BtnReset.Visible = false;//客户信息重置不允许
                    BtnSelectClient.Visible = false;//选择客户部允许
                    BtnGoToSetp2.Visible = false;//保存客户信息不允许
                    btnSelectTestItem.Visible = false;//选择测试项不允许
                    BtnFixContract.Visible = false;//签订合同按钮不允许
                    #endregion

                    #region 不可删除样品参数 不可移除检验依据 不可删减检验项目 不可上传原始记录
                    table2.Visible = true;
                    GridViewParametersFile.Columns[4].Visible = false;//不可删除参数文件
                    btnUploadParFile.Visible = false;                 //不准再上传参数文件
                    BtnUploadParFileHidden.Visible = false;


                    GridViewGbInfo.Columns[2].Visible = false;//不可移除检验依据
                    BtnSelectGbInfo.Visible = false;

                    GridViewCoTestItem.Columns[4].Visible = false;//不可删减检验项目

                    #endregion

                    #region 3，4，5不可改变
                    table3.Visible = true;//原始记录
                    GridViewTestRecords.Columns[6].Visible = false; //原始记录不可删除
                    table4.Visible = true; //检验报告

                    table5.Visible = true;//审核设置 

                    #endregion

                    #region 6 专家审核信息不可改
                    table6.Visible = true;//专家审核
                    TextBoxRem1.ReadOnly = true;
                    BtnConfirm1.Enabled = false;
                    BtnRefuse1.Enabled = false;
                    #endregion

                    table7.Visible = true;
                    #region 已整理完成的报告不能再修改
                    BtnToWaitConfirm1.Enabled = false;
                    BtnUploadReport.Enabled = false;
                    BtnSendBackTestGrp.Enabled = false;
                    BtnDeleteReport.Enabled = false;
                    BtnSelectExperts.Visible = false;
                    ButtonSelectMaster.Visible = false;
                    #endregion
                    break;
                }
            case ContractStatus.Done://审核完成
                {

                    #region  客户信息不可更改
                    table1.Visible = true;
                    table2.Visible = true;
                    textBoxCorp_Name.ReadOnly = true;
                    textBoxConsigner.ReadOnly = true;
                    textBoxLinkman.ReadOnly = true;
                    textBoxPhone.ReadOnly = true;
                    textBoxFax.ReadOnly = true;
                    textBoxEmail.ReadOnly = true;
                    BtnReset.Visible = false;//客户信息重置不允许
                    BtnSelectClient.Visible = false;//选择客户部允许
                    BtnGoToSetp2.Visible = false;//保存客户信息不允许
                    btnSelectTestItem.Visible = false;//选择测试项不允许
                    BtnFixContract.Visible = false;//签订合同按钮不允许
                    #endregion

                    #region 不可删除样品参数 不可移除检验依据 不可删减检验项目 不可上传原始记录
                    table2.Visible = true;
                    GridViewParametersFile.Columns[4].Visible = false;//不可删除参数文件
                    btnUploadParFile.Visible = false;                 //不准再上传参数文件
                    BtnUploadParFileHidden.Visible = false;


                    GridViewGbInfo.Columns[2].Visible = false;//不可移除检验依据
                    BtnSelectGbInfo.Visible = false;

                    GridViewCoTestItem.Columns[4].Visible = false;//不可删减检验项目

                    #endregion

                    #region 3，4，5不可改变
                    table3.Visible = true;//原始记录
                    GridViewTestRecords.Columns[6].Visible = false; //原始记录不可删除
                    table4.Visible = true; //检验报告

                    table5.Visible = true;//审核设置 

                    #endregion

                    #region 6 专家审核信息不可改
                    table6.Visible = true;//专家审核
                    BtnConfirm1.Enabled = false;
                    TextBoxRem1.ReadOnly = true;
                    BtnRefuse1.Enabled = false;
                    #endregion
                    table7.Visible = true;//最终审核

                    #region 7 最终审核信息不可变更
                    BtnConfirm2.Enabled = false;
                    TextBoxRem2.ReadOnly = true;
                    BtnRefuse2.Enabled = false;
                    #endregion
                    #region 已整理完成的报告不能再修改
                    BtnToWaitConfirm1.Enabled = false;
                    BtnUploadReport.Enabled = false;
                    BtnSendBackTestGrp.Enabled = false;
                    BtnDeleteReport.Enabled = false;
                    BtnSelectExperts.Visible = false;
                    ButtonSelectMaster.Visible = false;
                    #endregion
                    break;
                }
        }
        #endregion

        //1、加载客户信息 table1
        BindClientInfo(mo);
        #region 2、加载table2 的信息 样品参数 检验依据 检验项目
        if (table2.Visible)//检验项，依据，参数文件
        {
            btnSelectTestItem.Attributes.Add("OnClick", "InvokePopSetCTI('" + report_id + "')");
            BtnSelectGbInfo.Attributes.Add("OnClick", "InvokePopSetCGB('" + report_id + "')");
            btnUploadParFile.Attributes.Add("OnClick", "InvokePopUploadParFile('" + report_id + "')");


            //2、加载参数文件列表
            BindParameterFiles();
            //3、加载检验依据                  
            BindContractGB();
            //4、检验项目ID
            BindCoTestItem();

        }
        #endregion
        //3、加载原始记录
        if (table3.Visible)
        {
            BindTestRecords();
        }
        //4加载报告文件
        if (table4.Visible)
        {
            BindReportInfo(mo);
        }
        //审核设置
        if (table5.Visible)
        {
            LoadAuditSetting();
            BtnSelectExperts.Attributes.Add("OnClick",
                "InvokePopSelectAuditor('" + report_id + "','" + SiteConfig.REPORT_AUDIT_FLOW_1_EXPERT_AUDIT + "')");
            BtnSelectExperts.Attributes.Add("OnClick",
                "InvokePopSelectAuditor('" + report_id + "','" + SiteConfig.REPORT_AUDIT_FLOW_2_MASTER_AUDIT + "')");
        }
        //审核信息1
        if (table6.Visible)
        {
            LoadConirm1Rem();
        }
        //审核信息2
        if (table7.Visible)
        {
            LoadConfirm2Rem();
        }

    }





    #endregion

    #region 上传参数文件，选择检验项，选择检验依据
    /// <summary>
    /// 上传参数文件,一个报告，可以有多个参数文件，但应该只有一个Excel格式的可解析的参数文件.
    /// 目前要求参数文件中的标签是特殊格式的，才可以在多个报告中引用。2009-6-1
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUploadParFile_Click(object sender, EventArgs e)
    {
        //刷新参数
        BindParameterFiles();
    }

    /// <summary>
    /// 添加检验项目
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSelectTestItem_Click(object sender, EventArgs e)
    {
        BindCoTestItem();
    }

    /// <summary>
    /// 已经Popup选择完毕，刷新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnSelectGbInfo_Click(object sender, EventArgs e)
    {
        BindContractGB();
    }
    #endregion

    #region 移除参数文件，移除检验依据 ，移除原始记录文件

    /// <summary>
    /// 删除参数文件时提醒
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridViewParametersFile_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("GvDelete");
            l.Attributes.Add("onclick", "javascript:return " +
            "confirm(' 您确认删除参数文件 " +
            DataBinder.Eval(e.Row.DataItem, "FILE_NAME") + "，注意：文件删除后不可恢复，只能重新上传！ ')");
        }

    }

    /// <summary>
    /// 删除参数文件 ,目前，一个报告，1个参数文件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GvDeleteParametersFile_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string sample_id = (string)GridViewParametersFile.DataKeys[gvr.RowIndex].Value;
        dasp.DeleteByPK(sample_id);
        BindParameterFiles();
    }
    /// <summary>
    /// 移除检验依据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GvDeleteGbInfo_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string gb_code = (string)GridViewGbInfo.DataKeys[gvr.RowIndex].Value;
        string report_id = ViewState["REPORT_ID"].ToString();
        dacgb.Delete(gb_code, report_id);
        BindContractGB();
    }

    /// <summary>
    /// 删除合同检测项
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GvDeleteCoTestItem_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string co_item_id = (string)GridViewCoTestItem.DataKeys[gvr.RowIndex].Value;
        dacti.Delete(co_item_id);
        //TODO:删除检验项之后，需要更新总价

        BindCoTestItem();
    }
    protected void GvDeleteTestRecords_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string tr_id = (string)GridViewTestRecords.DataKeys[gvr.RowIndex].Value;
        MoTestRecord mo_tr = datr.GetEntity(tr_id);
        MoCoTestItem mo_coti = dacti.GetEntity(mo_tr.Co_Item_Id);
        if (mo_coti.Test_Status == (int)CoTestItemStatus.TestDone)
        {
            Utility.GlobalLog.WarnFormat("不允许删除已经完成的测试项的原始记录.{0},{1}", mo_tr, mo_coti);
            return;
        }

        MoTfile mo_xls = datf.GetEntity(mo_tr.Tr_Url);          //得到原始记录文件对象
        MoTfile mo_doc = datf.GetEntity(mo_tr.Report_Slice_Url);//报告片段文件对象

        try
        {
            if (mo_xls != null)
            {
                string file_path_xls = Utility.GetLocalFileFromUri(mo_xls.File_Url);
                System.IO.File.Delete(file_path_xls);//从文件系统删除原始文件
                datf.Delete(mo_xls);              //删除原始文件的文件记录
                Utility.GlobalLog.WarnFormat("删除原始记录文件{0}", mo_xls.File_Url);
            }

            if (mo_doc != null)
            {
                string file_path_doc = Utility.GetLocalFileFromUri(mo_doc.File_Url);
                System.IO.File.Delete(file_path_doc);//从文件系统删除报告片段文件
                datf.Delete(mo_doc);              //删除原始文件的文件记录
                Utility.GlobalLog.WarnFormat("删除原始记录文件{0}", mo_doc.File_Url);
            }
            datr.Delete(mo_tr);
            UserContext uc = Session["USER_CONTEXT"] as UserContext;
            DaActLog.LogOperation(uc.UserID, "NORMAL_ACT", String.Format("删除原始记录文件:{0}.{1}.", mo_tr, mo_coti));

        }
        catch (Exception ex)
        {
            Utility.GlobalLog.Error("删除文件失败!", ex);
            Utility.SiteError("删除文件失败！", Response);
        }
        BindTestRecords();
    }
    #endregion

    #region 插入合同的客户信息，并生成报告号，这时报告处于草稿状态
    /// <summary>
    /// 插入合同的客户信息，并生成报告号，这时报告处于草稿状态。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnGoToSetp2_Click(object sender, EventArgs e)
    {

        string client_id = Request.Form[textBoxClientID.UniqueID];// textBoxClientID.Text; 
        textBoxClientID.Text = client_id;
        //JavaScript没有更改视图状态
        if (String.IsNullOrEmpty(client_id))
        {
            LabelContractStatus.Text = "请首先选择一个有效的客户！";
            return;
        }
        else
        {
            MoClientInfo mo = daci.GetEntity(client_id);//客户信息
            if (mo == null)
            {
                LabelContractStatus.Text = "客户ID无效！";
                Utility.SiteError(String.Format("无效的客户编号:{0}", client_id));
                return;
            }
            else
            {
                //textBoxClientID.Text = client_id;
                MoContract mocontract = null;
                bool isUpdate = false; //是更新吗？

                if (ViewState["REPORT_ID"] == null)
                {
                    isUpdate = false;
                    mocontract = new MoContract();
                    try
                    {
                        mocontract.Report_Id = SequenceKeys.GenerateContractID();
                    }
                    catch (DalException ex)
                    {
                        Utility.GlobalLog.Error("新建合同失败！", ex);
                        Utility.SiteError(ex.Message);
                        return;
                    }

                }
                else
                {
                    isUpdate = true;
                    mocontract = dacontract.GetEntity(ViewState["REPORT_ID"].ToString());
                    if (mocontract == null)
                    {
                        Utility.SiteError(String.Format("给定的报告编号{0}无效.", ViewState["REPORT_ID"]));
                        return;
                    }
                }

                UserContext uc = Session["USER_CONTEXT"] as UserContext;
                if (uc != null)
                {
                    mocontract.Uid = uc.UserID;
                }
                else
                {
                    Utility.SiteError("无法从会话中获取用户上下文信息！", Page.Response);
                    return;
                }

                mocontract.Client_Id = client_id;
                mocontract.Corp_Name = textBoxCorp_Name.Text;
                mocontract.Consigner = textBoxConsigner.Text;
                mocontract.Linkman = textBoxLinkman.Text;
                mocontract.Tel = mo.Tel;
                mocontract.Phone = textBoxPhone.Text;
                mocontract.Fax = textBoxFax.Text;
                mocontract.Addr = mo.Addr;
                mocontract.Postcode = mo.Postcode;
                mocontract.Email = mo.Email;
                mocontract.Charge = 0;
                mocontract.Report_Status = (int)ContractStatus.Draft;//草稿状态
                mocontract.Total_Price = 0;
                try
                {
                    if (isUpdate)
                    {
                        dacontract.Update(mocontract);
                    }
                    else
                    {
                        dacontract.Add(mocontract);
                        ViewState["REPORT_ID"] = mocontract.Report_Id;
                    }
                    SuitUI2ReportStatus(mocontract);
                }
                catch (DalException ex)
                {
                    Utility.GlobalLog.Error("新建合同失败！", ex);
                    Utility.SiteError(ex.Message);
                    return;
                }
            }
        }
    }
    #endregion

    #region  BtnFixContract_Click 确认签订合同
    /// <summary>
    /// 确认合同
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnFixContract_Click(object sender, EventArgs e)
    {
        //更新合同状态 从草稿1000到已签订/待送检1010
        string report_id = ViewState["REPORT_ID"].ToString();
        dacontract.ReplaceContractStatus(report_id, (int)ContractStatus.Signed);
        MoContract mo = dacontract.GetEntity(report_id);
        SuitUI2ReportStatus(mo);

    }
    #endregion

    #region BtnTopReturn_Click 返回按钮
    protected void BtnTopReturn_Click(object sender, EventArgs e)
    {

        Response.Redirect(ViewState["UrlReferrer"].ToString());
    }

    #endregion

    #region BtnDeleteReport_Click 删除当前已经上传的报告
    /// <summary>
    /// 删除当前已经上传的报告
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnDeleteReport_Click(object sender, EventArgs e)
    {
        string report_id = ViewState["REPORT_ID"].ToString();
        if (!String.IsNullOrEmpty(report_id))
        {
            dacontract.DeleteContractReport(report_id);
            MoContract m = dacontract.GetEntity(report_id);
            BindReportInfo(m);
            UserContext uc = Session["USER_CONTEXT"] as UserContext;
            DaActLog.LogOperation(uc.UserID, "NORMAL_ACT", String.Format("删除报告:{0}.", report_id));

        }
    }
    #endregion

    #region BtnSelectAuditor_Click 绑定审批人
    /// <summary>
    /// 绑定审批人
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnSelectAuditor_Click(object sender, EventArgs e)
    {
        LabelExperts.Text = Server.HtmlEncode(dara.GetReportAuditorNames(ViewState["REPORT_ID"].ToString(),
            SiteConfig.REPORT_AUDIT_FLOW_1_EXPERT_AUDIT));

        LabelMasters.Text = Server.HtmlEncode(dara.GetReportAuditorNames(ViewState["REPORT_ID"].ToString(),
            SiteConfig.REPORT_AUDIT_FLOW_2_MASTER_AUDIT));


    }
    #endregion

    #region BtnUploadReport_Click 上传报告文件
    /// <summary>
    /// 先弹出窗口上传文件，然后刷新。这里实际是刷新代码。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnUploadReport_Click(object sender, EventArgs e)
    {
        string report_id = ViewState["REPORT_ID"].ToString();
        MoContract m = dacontract.GetEntity(report_id);
        BindReportInfo(m);
        UserContext uc = Session["USER_CONTEXT"] as UserContext;
        DaActLog.LogOperation(uc.UserID, "NORMAL_ACT", String.Format("上传报告:{0}.", m));

    }
    #endregion

    #region btnHiddenAddTRFile_Click 添加原始记录文件
    /// <summary>
    /// 添加原始记录文件之后，刷新原始记录文件列表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnHiddenAddTRFile_Click(object sender, EventArgs e)
    {
        BindTestRecords();
    }
    #endregion

    #region BtnRemoveContract_Click 删除合同
    /// <summary>
    /// 删除合同
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnRemoveContract_Click(object sender, EventArgs e)
    {
        if (ViewState["REPORT_ID"] != null)
        {
            string report_id = ViewState["REPORT_ID"].ToString();
            MoContract mo = dacontract.GetEntity(report_id);
            DaActLog.LogNormalAct(User.Identity.Name, String.Format("删除了合同:{0}", mo));
            if (mo != null && (mo.Report_Status == (int)ContractStatus.Draft || mo.Report_Status == (int)ContractStatus.Signed)) //只能删除草稿状态或者已签订状态的合同，即试验未进行
            {
                dacontract.DeleteContractEx(report_id);
                ViewState.Remove("REPORT_ID");
                Response.Redirect(ViewState["UrlReferrer"].ToString());
            }
            else
            {
                Utility.SiteError(String.Format("给定的报告编号{0}无法删除，数据结构不完整，或者报告状态已经不允许删除！", report_id));
            }

        }
    }
    #endregion

    #region BtnConfirmTaskDone_Click  检验组长确认报告任务完成
    /// <summary>
    /// 检验组长确认报告任务完成
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnConfirmTaskDone_Click(object sender, EventArgs e)
    {
        //将本组负责的各个检验项状态更改为最终完成
        UserContext uc = Session["USER_CONTEXT"] as UserContext;
        string report_id = ViewState["REPORT_ID"].ToString();
        dacti.UpdateCoTestItemGroupState(report_id, uc.DeptID, uc.UserID, CoTestItemStatus.TaskDone);
        //刷新合同检验项
        //BindCoTestItem();
        //BindTestRecords();
        SuitUI2ReportStatus(dacontract.GetEntity(ViewState["REPORT_ID"].ToString()));

    }
    #endregion

    #region 报告组退回实验组，即更新所有试验为未完成，且更新报告的状态为 检验中
    /// <summary>
    /// 退回实验组，即更新所有试验为未完成
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnSendBackTestGrp_Click(object sender, EventArgs e)
    {
        //TODO:应该事务性完成

        dacti.UpdateReportCoTestItemsState(ViewState["REPORT_ID"].ToString(), (int)CoTestItemStatus.Testing);
        //更新报告的状态为 试验中
        dacontract.ReplaceContractStatus(ViewState["REPORT_ID"].ToString(), (int)ContractStatus.Testing);
        SuitUI2ReportStatus(dacontract.GetEntity(ViewState["REPORT_ID"].ToString()));
    }
    #endregion

    //绑定合同检验项列表时，设定按钮列客户端脚本
    protected void GridViewCoTestItem_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //检查此行的状态

            DataRowView drv = e.Row.DataItem as DataRowView;
            int ts = (int)drv["TEST_STATUS"];
            LinkButton btn2 = (LinkButton)e.Row.FindControl("LBDownloadTemplate");
            LinkButton btn3 = (LinkButton)e.Row.FindControl("LBUploadTestRecord");

            if (ts < (int)CoTestItemStatus.TestDone)
            {
                btn2.Visible = true;
                btn3.Visible = true;
                btn2.Attributes.Add("OnClick", "InvokePopDownloadTemplate('" + drv["Item_Code"] + "','" + drv["Co_Item_ID"] + "')");
                btn3.Attributes.Add("OnClick", "InvokePop('" + drv["Co_Item_ID"] + "')");
            }
            else
            {
                btn2.Visible = false;
                btn3.Visible = false;
            }
        }
    }


    /// <summary>
    /// 原始记录绑定每一行时，应判定检验项的状态，若检验项已经完成了，此原始记录不准删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridViewTestRecords_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = e.Row.DataItem as DataRowView;
            int test_state = (int)drv["Test_Status"];
            //如果检验项已经测试完成，则不可再删除
            if ((test_state & (int)CoTestItemStatus.TestDone) > 0 || (test_state & (int)CoTestItemStatus.TaskDone) > 0)//已经测试完成
            {
                LinkButton lb = e.Row.FindControl("GvDeleteTestRecords") as LinkButton;
                if (lb != null)
                {
                    lb.Enabled = false;
                }
            }

        }

    }


    #region 报告整理完成
    /// <summary>
    /// 报告整理完成
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnToWaitConfirm1_Click(object sender, EventArgs e)
    {
        dacontract.ReplaceContractStatus(ViewState["REPORT_ID"].ToString(),
            (int)ContractStatus.WaitConfirm1);
        //专家审核中状态无需使用
        #region  发消息给相关专家，每个专家都会收到消息

        UserContext uc = Session["USER_CONTEXT"] as UserContext;

        Collection<MoReportAudit> ras = dara.GetEntities(String.Format(" WHERE Report_ID='{0}' AND Flow_Code='{1}'", ViewState["REPORT_ID"], SiteConfig.REPORT_AUDIT_FLOW_1_EXPERT_AUDIT));
        if (ras != null && ras.Count > 0)
        {
            foreach (MoReportAudit m in ras)
            {
                MoNotice n = new MoNotice();
                n.Notice_Id = SequenceKeys.GenerateNoticeID();
                n.Uid = uc.UserID;
                n.Publish_Type = 1;
                n.Title = "报告审核通知";
                n.Create_Time = DateTime.Now;
                n.Notice_Content = String.Format("{0}:{1}[{2}]请求审核合同{3}的报告文件.", DateTime.Now, uc.EmpName, uc.UserID, ViewState["REPORT_ID"]);
                if (danotice.CreateNotice(n, m.AuditorId))
                {
                    Utility.SaveNoticeContent(n.Notice_Id, n.Notice_Content);
                }
            }
        }
        #endregion

        SuitUI2ReportStatus(dacontract.GetEntity(ViewState["REPORT_ID"].ToString()));


    }
    #endregion

    #region 审核 1，2
    protected void BtnConfirm1_Click(object sender, EventArgs e)
    {
        //更新报告的状态 为 专家审核通过
        dacontract.ReplaceContractStatus(ViewState["REPORT_ID"].ToString(), (int)ContractStatus.WaitLastConfirm);
        UserContext uc = Session["USER_CONTEXT"] as UserContext;
        if (uc != null)
        {
            dara.UpdateReportAuditRem(ViewState["REPORT_ID"].ToString(), SiteConfig.REPORT_AUDIT_FLOW_1_EXPERT_AUDIT,
                String.Format("{0}  {1}/{2}", TextBoxRem1.Text, uc.EmpName, DateTime.Now), uc.UserID);
        }


        //专家审核按钮不再有效
        //BtnConfirm1.Enabled = false;
        //TextBoxRem1.ReadOnly = true;
        //BtnRefuse1.Enabled = false;
        SuitUI2ReportStatus(dacontract.GetEntity(ViewState["REPORT_ID"].ToString()));
        //TODO:发消息给报告组，告知他为什么退回/同意





    }

    #region 专家否决报告文件
    /// <summary>
    /// 否决报告
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnRefuse1_Click(object sender, EventArgs e)
    {
        //更新报告的状态 为等待审核
        dacontract.ReplaceContractStatus(ViewState["REPORT_ID"].ToString(), (int)ContractStatus.Reporting);
        //删除所有旧的审核设定
        dara.DeleteByWhereClause(String.Format(" WHERE REPORT_ID ='{0}'", ViewState["REPORT_ID"]));

        SuitUI2ReportStatus(dacontract.GetEntity(ViewState["REPORT_ID"].ToString()));

        #region 发消息
        MoContract c = dacontract.GetEntity(ViewState["REPORT_ID"].ToString());
        if (c != null)
        {
            MoDirFile modf = dadf.GetEntity(c.Report_Url);
            if (modf != null)
            {
                UserContext uc = Session["USER_CONTEXT"] as UserContext;
                //发消息给报告创建人
                MoNotice n = new MoNotice();
                n.Notice_Id = SequenceKeys.GenerateNoticeID();
                n.Uid = uc.UserID;
                n.Publish_Type = 1;
                n.Title = "通知:报告审核未通过";
                n.Create_Time = DateTime.Now;
                n.Notice_Content = String.Format("{0}:{1}[{2}]否决了合同{3}的报告文件，审批意见如下：", DateTime.Now, uc.EmpName, uc.UserID, ViewState["REPORT_ID"], TextBoxRem1.Text);

                if (danotice.CreateNotice(n, modf.Uid))
                {
                    Utility.SaveNoticeContent(n.Notice_Id, n.Notice_Content);
                }
            }
        }
        #endregion
    }
    #endregion

    /// <summary>
    /// 站长同意，此时合同所有信息不可再更改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnConfirm2_Click(object sender, EventArgs e)
    {
        //更新报告的状态 为 专家审核通过
        dacontract.ReplaceContractStatus(ViewState["REPORT_ID"].ToString(), (int)ContractStatus.Done);
        UserContext uc = Session["USER_CONTEXT"] as UserContext;
        if (uc != null)
        {
            dara.UpdateReportAuditRem(ViewState["REPORT_ID"].ToString(), SiteConfig.REPORT_AUDIT_FLOW_2_MASTER_AUDIT,
                String.Format("{0}  {1}/{2}", TextBoxRem2.Text, uc.EmpName, DateTime.Now), uc.UserID);
        }
        //站长审核按钮不再有效
        SuitUI2ReportStatus(dacontract.GetEntity(ViewState["REPORT_ID"].ToString()));
    }

    /// <summary>
    /// 站长否决
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnRefuse2_Click(object sender, EventArgs e)
    {
        //更新报告的状态 为等待审核
        dacontract.ReplaceContractStatus(ViewState["REPORT_ID"].ToString(), (int)ContractStatus.Reporting);
        //删除所有旧的审核设定
        dara.DeleteByWhereClause(String.Format(" WHERE REPORT_ID ='{0}'", ViewState["REPORT_ID"]));

        SuitUI2ReportStatus(dacontract.GetEntity(ViewState["REPORT_ID"].ToString()));
        #region 发消息
        MoContract c = dacontract.GetEntity(ViewState["REPORT_ID"].ToString());
        if (c != null)
        {
            MoDirFile modf = dadf.GetEntity(c.Report_Url);
            if (modf != null)
            {
                UserContext uc = Session["USER_CONTEXT"] as UserContext;
                //发消息给报告创建人
                MoNotice n = new MoNotice();
                n.Notice_Id = SequenceKeys.GenerateNoticeID();
                n.Uid = uc.UserID;
                n.Publish_Type = 1;
                n.Title = "通知:报告审核未通过";
                n.Create_Time = DateTime.Now;
                n.Notice_Content = String.Format("{0}:{1}[{2}]否决了合同{3}的报告文件，审批意见如下：", DateTime.Now, uc.EmpName, uc.UserID, ViewState["REPORT_ID"], TextBoxRem2.Text);

                if (danotice.CreateNotice(n, modf.Uid))
                {
                    Utility.SaveNoticeContent(n.Notice_Id, n.Notice_Content);
                }
            }
        }
        #endregion
    }
    #endregion

}
